<?php


namespace Biz\model\advertisement;

use Medoo\Medoo;

/**
 * @property null $occupied
 */
class MHhzDepartment extends MBaseDbModel
{
    const _CONFIG_FILE_ = 'db_position';

    const _CONFIG_SELECT_ = 'db_position';

    const _DATABASE_ = 'hhz_position';

    const _TABLE_NAME = 'hhz_department';

    public static function getAll()
    {
        $where = [
            'GROUP' => [self::_TABLE_NAME . '.id'],
            "OR #the first condition" => [
                MHhzDepartmentMenu::_TABLE_NAME . '.is_deleted #the first condition' => MHhzDepartmentMenu::IS_DELETED_NO,
                MHhzDepartmentMenu::_TABLE_NAME . '.is_deleted #the second condition' => null,
            ],
            "OR #the second condition" => [
                MHhzDepartmentAdType::_TABLE_NAME . '.is_deleted #the first condition' => MHhzDepartmentAdType::IS_DELETED_NO,
                MHhzDepartmentAdType::_TABLE_NAME . '.is_deleted #the second condition' => null,
            ],
            "OR #the third condition" => [
                self::_TABLE_NAME . '.status #the first condition' => 0,
                self::_TABLE_NAME . '.status #the second condition' => null,
            ],
        ];

        return self::select([
            "[>]" . MHhzAdmin::_TABLE_NAME => ["manager_id" => "id"],
            "[>]" . MHhzDepartmentMenu::_TABLE_NAME => ["id" => "department_id"],
            "[>]" . MHhzDepartmentAdType::_TABLE_NAME => ["id" => "department_id"],
        ],
            [
                self::_TABLE_NAME . '.id',
                self::_TABLE_NAME . '.name',
                self::_TABLE_NAME . '.manager_id',
                self::_TABLE_NAME . '.wx_tag_id',
                self::_TABLE_NAME . '.occupied',
                MHhzAdmin::_TABLE_NAME . '.account',
                'menu_codes' => Medoo::raw('group_concat(distinct(' . MHhzDepartmentMenu::_TABLE_NAME . '.menu_code))'),
                'ad_type_ids' => Medoo::raw('group_concat(distinct(' . MHhzDepartmentAdType::_TABLE_NAME . '.ad_type_id))'),
            ], $where);
    }

    public static function edit($id, $data)
    {
        if (!$id) {
            return;
        }

        MHhzDepartment::update($data, ['id' => $id]);
    }

    public static function create($data)
    {
        $insertData = [
            'name' => $data['name'],
            'manager_id' => $data['manager_id'],
            'wx_tag_id' => $data['wx_tag_id'],
        ];

        MHhzDepartment::insert($insertData);
    }
}
